
function [T,x,P,pi,fval]  = eval_1s_2c_gov_ss_nogL(xval)

% for multi-sectors
global rbest  L theta rbest ns n alpha betas d sigma ilog_rL delta3 beta

% guess wage and share of labor in each sector

w(1:n,1) = xval(1:n); % row vector
extax = xval(n+1);
taf =0;

rL = rbest*L;

T = alpha.*rL/delta3;

sextax = zeros(n);
for i=1:n-1
    sextax(i+1) = extax;
end
staf = zeros(n);

for i=1:n-1
    staf(i+1) = taf;
end

pi = zeros(n,n); % (1,2,3): country 2 export to ecountry 1 in sector 3, NOTE country order is opposite to our note
%d, matrix of n times n, dij country j ship to country i


xnimatrix1= T'.*((w'.*(1+sextax).*(1+staf).*d).^(-theta));
 xn1 =sum(xnimatrix1,2).*ones(n,n);

pimatrix1 = xnimatrix1./xn1;
pi = pimatrix1;
xnimatrix= xnimatrix1;


x  =  (1+theta)/theta*w.*(L-sum(rL,2));

extax1 = sextax;
for m=2:n
    x(1)=x(1)+betas(1)*extax1(m)/(1+extax)*pi(m,1)*x(m);
end
tempt=0;

for m=2:n
    tempt=tempt+betas(1)*taf/(1+taf)*pi(1,m);
end

x(1)=x(1)/(1-tempt);

% tax_income = betas_ns(2:end,:).*(extax1./(1+extax1).*squeeze(pi(2:end,1,:))).*x_ns(2:end,:);
% x(1) = x(1)+sum(tax_income(:));


% for m=1:n % destination country
%     for j=1:n
%         pval0(j)= xnimatrix(m,j); % shipped from j to m
%     end
%     %pval1 = (sum(squeeze(xnimatrix(m,:)),2)).^(-betas./theta);
%     pval1 = (sum(pval0,1)).^(-betas./theta);
%     %tp0 = cumprod(pval1);
%     P(m,1) =pval1;
% end

P=(sum(xnimatrix1,2)).^(-betas./theta);

fval(1) = P(1)-1;
fval(2) = extax-1/(theta*pi(2,2));
fval(3) = pi(1,2)*x(1)-pi(2,1)*x(2);
